package handler

import (
	"context"
	"emicro_test/emicro_8/grpc_gateway/common"
	"emicro_test/emicro_8/grpc_gateway/utils"
	"emicro_test/emicro_8/grpc_gateway/vendors/rbac"
	"encoding/json"
	"errors"
	"fmt"
	"github.com/julienschmidt/httprouter"
	"github.com/asim/go-micro/v3/metadata"
	"io/ioutil"
	"log"
	"net/http"
	pb "emicro_test/emicro_8/grpc_gateway/pbfile"
)


//获取用户信息
//Get请求参数直接再params取就可以了
func TestUserGet(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	//验证token
	token := r.Header.Get("Authorization")
	log.Println("token:",token)
	if err :=utils.VerityToken(token); err!=nil{
		http.Error(w, err.Error(), 500)
		return
	}

	//解析token参数
	user_id, role_id, err := utils.GetJwtData(token)
	if err !=nil{
		http.Error(w, err.Error(), 500)
		return
	}

	//权限验证
	if !rbac.RbacFilter(role_id, "user.TestUser") {
		http.Error(w, errors.New("not permission").Error(), 500)
		return
	}

	//请求micro的用户服务
	service := pb.NewUserService("emicro.user", common.Service.Client())
	ctx := metadata.Set(context.Background(), "token", token)
	resp, err := service.TestUser(ctx, &pb.TestReq{Id:int32(user_id)})
	if err !=nil{
		log.Println("TestUserGet err:", err)
		http.Error(w, err.Error(), 500)
		return
	}
	log.Println(user_id, resp.Msg, params)
	w.Write([]byte(resp.Msg))
}

//获取用户信息1
//Post请求参数要解析
func TestUserPost(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	//解析post参数
	var request map[string]interface{}
	if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
		log.Println("TestUserPost ParseForm err:", err)
		http.Error(w, err.Error(), 500)
		return
	}
	user_id := utils.GetInt(request["user_id"])

	//请求micro的用户服务
	service := pb.NewUserService("emicro.user", common.Service.Client())
	resp, err := service.TestUser(context.Background(), &pb.TestReq{Id:int32(user_id)})
	if err !=nil{
		log.Println("TestUserGet err:", err)
		http.Error(w, err.Error(), 500)
		return
	}
	log.Println(user_id, resp.Msg)
	w.Write([]byte(resp.Msg))
}

//注册接口
func RoleReg(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	//解析参数
	var request map[string]interface{}
	if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
		log.Println("RoleReq ParseForm err:", err)
		http.Error(w, err.Error(), 500)
		return
	}

	log.Println(request)
	service := pb.NewUserService("emicro.user", common.Service.Client())
	resp, err := service.UserReg(context.Background(), &pb.RegReq{
		Name: utils.GetString(request["name"]),
		Phone:utils.GetString(request["phone"]),
		Email: utils.GetString(request["email"]),
		Pwd: utils.GetString(request["pwd"]),
		ConfirPwd: utils.GetString(request["confire_pwd"]),
	})
	if err !=nil{
		log.Println("RoleReq err:", err)
		http.Error(w, err.Error(), 500)
		return
	}
	log.Println(resp.Status, resp.Msg)
	res := fmt.Sprintf("status:%d,msg:%v", resp.Status, resp.Msg)
	w.Write([]byte(res))
}

//登录接口
func RoleLogin(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	//解析参数
	var request map[string]interface{}
	if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
		log.Println("RoleLogin ParseForm err:", err)
		http.Error(w, err.Error(), 500)
		return
	}

	//log.Println(request)
	service := pb.NewUserService("emicro.user", common.Service.Client())
	resp, err := service.UserLogin(context.Background(), &pb.LoginReq{
		Phone:utils.GetString(request["phone"]),
		Pwd: utils.GetString(request["pwd"]),
	})
	if err !=nil{
		log.Println("RoleLogin err:", err)
		http.Error(w, err.Error(), 500)
		return
	}
	//log.Println(resp.Status, resp.Token, resp.Msg)
	res := fmt.Sprintf("status:%d,user_id:%v,msg:%v", resp.Status, resp.Token, resp.Msg)
	ioutil.WriteFile("log.txt", []byte(string(resp.Token)), 0655)
	w.Write([]byte(res))
}